package cbfg.butil.demo

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import cbfg.butil.KLog
import cbfg.butil.demo.databinding.FragmentKlogBinding

/**
 * @author: Tom Hawk
 * 添加时间: 2020/9/4 17:32
 * 功能描述:
 */
class KKLogFragment : Fragment(R.layout.fragment_klog),
    View.OnClickListener {
    private var _binding: FragmentKlogBinding? = null
    private val binding: FragmentKlogBinding
        get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = super.onCreateView(inflater, container, savedInstanceState)!!
        _binding = FragmentKlogBinding.bind(view)
        return view
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onViewCreated(
        view: View,
        savedInstanceState: Bundle?
    ) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnLogDebug.setOnClickListener(this)
        binding.btnLog.setOnClickListener(this)
        binding.btnLogWithNull.setOnClickListener(this)
        binding.btnLogWithMsg.setOnClickListener(this)
        binding.btnLogWithLong.setOnClickListener(this)
        binding.btnLogWithTag.setOnClickListener(this)
        binding.btnLogWithParams.setOnClickListener(this)
        binding.btnLogWithJson.setOnClickListener(this)
        binding.btnLogWithLongJson.setOnClickListener(this)
        binding.btnLogWithJsonTag.setOnClickListener(this)
        binding.btnLogWithFile.setOnClickListener(this)
        binding.btnLogWithXml.setOnClickListener(this)

        JSON_LONG = resources.getString(R.string.json_long)
        JSON = resources.getString(R.string.json)
        STRING_LONG = Utils.getLongTxt(view.context)

        handler.sendEmptyMessageDelayed(0, 3000)
    }

    private fun logDebug() {
        KLog.d("This is a debug message")
        KLog.d("DEBUG", "params1", "params2", this)
    }

    private fun log() {
        TestKLog.v()
        TestKLog.d()
        TestKLog.i()
        TestKLog.w()
        TestKLog.e()
        TestKLog.a()
    }

    private fun logWithNull() {
        KLog.v(null)
        KLog.d(null)
        KLog.i(null)
        KLog.w(null)
        KLog.e(null)
        KLog.a(null)
    }

    private fun logWithMsg() {
        KLog.v(LOG_MSG)
        KLog.d(LOG_MSG)
        KLog.i(LOG_MSG)
        KLog.w(LOG_MSG)
        KLog.e(LOG_MSG)
        KLog.a(LOG_MSG)
    }

    private fun logWithTag() {
        KLog.v(TAG, LOG_MSG)
        KLog.d(TAG, LOG_MSG)
        KLog.i(TAG, LOG_MSG)
        KLog.w(TAG, LOG_MSG)
        KLog.e(TAG, LOG_MSG)
        KLog.a(TAG, LOG_MSG)
    }

    private fun logWithLong() {
        KLog.d(TAG, STRING_LONG)
    }

    private fun logWithParams() {
        KLog.v(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
        KLog.d(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
        KLog.i(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
        KLog.w(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
        KLog.e(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
        KLog.a(
            TAG,
            LOG_MSG,
            "params1",
            "params2",
            this
        )
    }

    private fun logWithJson() {
        KLog.json("12345")
        KLog.json(null)
        KLog.json(JSON)
    }

    private fun logWithLongJson() {
        KLog.json(JSON_LONG)
    }

    private fun logWithJsonTag() {
        KLog.json(TAG, JSON)
    }

    private fun logWithFile(view: View) {
        KLog.file(
            null,
            JSON_LONG,
            view.context.externalCacheDir!!,
            null,
            true
        )
        KLog.file(
            TAG,
            JSON_LONG,
            view.context.externalCacheDir!!,
            null,
            true
        )
        KLog.file(
            TAG,
            JSON_LONG,
            view.context.externalCacheDir!!,
            "test.txt",
            true
        )
    }

    private fun logWithXml() {
        KLog.xml("12345")
        KLog.xml(null)
        KLog.xml(XML)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.btnLogDebug -> logDebug()
            R.id.btnLog -> log()
            R.id.btnLogWithNull -> logWithNull()
            R.id.btnLogWithMsg -> logWithMsg()
            R.id.btnLogWithLong -> logWithLong()
            R.id.btnLogWithTag -> logWithTag()
            R.id.btnLogWithParams -> logWithParams()
            R.id.btnLogWithJson -> logWithJson()
            R.id.btnLogWithLongJson -> logWithLongJson()
            R.id.btnLogWithJsonTag -> logWithJsonTag()
            R.id.btnLogWithFile -> logWithFile(view)
            R.id.btnLogWithXml -> logWithXml()
        }
    }

    companion object {
        private const val LOG_MSG = "KLog is a so cool Log Tool!"
        private const val TAG = "KLog"
        private const val XML =
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><!--  Copyright w3school.com.cn --><note><to>George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting!</body></note>"
        private lateinit var JSON: String
        private lateinit var JSON_LONG: String
        private lateinit var STRING_LONG: String
        private val handler: Handler = object : Handler() {
            override fun handleMessage(msg: Message) {
                KLog.d("Inner Class Test")
            }
        }
    }
}